﻿using Microsoft.SqlServer.Types;
using System;
using MyFloridaHouse.libSvDTO;

namespace MyFloridaHouse.libServiceBusinessRules
{
    class BoundingBoxHelpers
    {
        // coordinates to box and back again from http://barendgehrels.blogspot.com/2011/05/sqlgeometry-and-boxes.html
                
        //public static SqlGeometry CoordinatesToBox(double west, double south, double east, double north, int srid)        
        //{           
        //    SqlGeometryBuilder builder = new SqlGeometryBuilder();
        //    builder.SetSrid(srid);           
        //    builder.BeginGeometry(OpenGisGeometryType.Polygon);
        //    builder.BeginFigure(west, south);
        //    builder.AddLine(west, north);
        //    builder.AddLine(east, north);
        //    builder.AddLine(east, south);
        //    builder.AddLine(west, south); // Yes, we close it neatly
        //    builder.EndFigure();           
        //    builder.EndGeometry();           
        //    return builder.ConstructedGeometry;        
        //}        
        
        //public static void BoxToCoordinates(SqlGeometry box, out double west, out double south, out double east, out double north)        
        //{           
        //    SqlGeometry lower_left = box.STPointN(1);           
        //    SqlGeometry upper_right = box.STPointN(3);          
        //    west = lower_left.STX.Value;
        //    south = lower_left.STY.Value;
        //    east = upper_right.STX.Value;
        //    north = upper_right.STY.Value;
        //    // Defensive programming:           
        //    MakeFirstSmaller(ref west, ref east);           
        //    MakeFirstSmaller(ref south, ref north);        
        //}
        
        private static void MakeFirstSmaller<T>(ref T a, ref T b) where T : IComparable        
            {           
                if (a.CompareTo(b) > 0)           
                {               
                    // Exchange the two values               
                    T t = a;               
                    a = b;               
                    b = t;           
                }        
        }        

        public static BoundingBoxDTO GeometrySTEnvelopeToBox(SqlGeometry box)
        {
            box.MakeValid();
            SqlGeometry lower_left = box.STPointN(1);
            SqlGeometry upper_right = box.STPointN(3);
            double west = lower_left.STX.Value;
            double south = lower_left.STY.Value;
            double east = upper_right.STX.Value;
            double north = upper_right.STY.Value;
            // Defensive programming:           
            MakeFirstSmaller(ref west, ref east);
            MakeFirstSmaller(ref south, ref north);

            BoundingBoxDTO result = new BoundingBoxDTO(north, west, south, east);

            return result;
        }

        
        public static double AreaFromBoundingBox(BoundingBoxDTO boundingBox)
        {
            return ((boundingBox.northWest.latitude - boundingBox.southEast.latitude) * (boundingBox.northWest.longitude - boundingBox.southEast.longitude));
        }
    }
}
